package org.jcut.controller;

import java.util.Date;
import java.util.UUID;

import org.jcut.bean.Collect;
import org.jcut.data.Data;
import org.jcut.service.ICollectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

@RestController
@RequestMapping("/collect")
public class CollectController {
	@Autowired
	private ICollectService service;
	@Autowired
	private Data data;

	/**
	 * @author 胡琪 String uId 用户id String cId 商品id
	 */
	@RequestMapping(value = "/add", params = { "uId", "cId" })
	public Object insert(@RequestParam(value = "uId", required = true) String uId,
			@RequestParam(value = "cId", required = true) String cId) {
		Collect collect = new Collect();
		collect.setId(UUID.randomUUID().toString());
		collect.setTime(new Date());
		collect.setuId(uId);
		collect.setcId(cId);
		collect.setDis(Integer.parseInt("1"));
		int i = service.insert(collect);
		if (i > 0) {
			data.setData("1");
			data.setMsg("添加成功！");
			data.setStatus("1");

		} else {
			data.setData("0");
			data.setMsg("服务器异常！");
			data.setStatus("0");

		}
		return data;

	}

	/**
	 * @author 胡琪 
	 * String id 收藏id
	 */
	@RequestMapping(value = "/delete")
	public Object delete(Collect coolect) {
		int i = service.deleteByPrimaryKey(coolect);
		if (i > 0) {
			data.setData("1");
			data.setMsg("删除成功！");
			data.setStatus("1");
		} else {
			data.setData("0");
			data.setMsg("服务器异常！");
			data.setStatus("0");

		}
		return data;
	}

	/**
	 * @author 胡琪
	 * @param String id 收藏id 
	 * @param String uId 用户id 
	 * @param String cId 商品id
	 * 
	 */
	@RequestMapping(value = "/update", params = { "id", "uId", "cId" })
	public Object update(@RequestParam("uId") String uId, @RequestParam("cId") String cId,
			@RequestParam("id") String id) {
		Collect collect = new Collect();
		collect.setDis(Integer.parseInt("1"));
		collect.setTime(new Date());
		collect.setcId(cId);
		collect.setuId(uId);
		collect.setId(id);
		int i = service.updateByPrimaryKey(collect);
		if (i > 0) {
			data.setData("1");
			data.setMsg("修改成功！");
			data.setStatus("1");
		} else {
			data.setData("0");
			data.setMsg("服务器异常！");
			data.setStatus("0");

		}
		return data;
	}

	/**
	 * @author 胡琪 /**
	 * 
	 *
	 * @param pageNum  页码
	 * @param pageSize 每页显示数量 
	 * String uId 用户id（必填）
	 * 
	 */
	@RequestMapping(value = "/select")
	public Object select(@RequestParam("uId") String uId,
			@RequestParam(value = "pageNum", required = false) String pageNum,
			@RequestParam(value = "pageSize", required = false) String pageSize) {
		if ((pageNum != null && !("".equals(pageNum))) && (pageSize != null && !("".equals(pageSize)))) {
			PageHelper.startPage(Integer.parseInt(pageNum), Integer.parseInt(pageSize));
		}
		PageInfo<Collect> pageInfo = new PageInfo<>(service.selectByUid(uId));
		data.setData(pageInfo);
		data.setMsg("查询成功！");
		data.setStatus("1");
		return data;

	}

	/**
	 * @author 胡琪
	 *  
	 *  String uId 用户id 
	 *  String cId 商品id
	 * 
	 */
	@RequestMapping("/userSelect")
	public Object userSelect(Collect collect) {
		long i=service.userSelect(collect);
		data.setData(i);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}
	@RequestMapping("/selectBycId")
	public Object selectById(String cId) {
		long i=service.selectById(cId);
		data.setData(i);
		data.setMsg("查询成功");
		data.setStatus("1");
		return data;
	}

}
